import { Component,Injectable} from '@angular/core';
import {CourseService} from './course.service'
import {Course} from '../../services/model/course'
import {PopService} from 'dolphinng'
import {Grade} from "../../services/model/grade";
import {Major} from "../../services/model/major";
@Component({
    selector: 'course',
    templateUrl: './course.component.html',
    styleUrls: ['./course.component.less'],
    providers:[CourseService]
})
@Injectable()
export class CourseComponent {

  tableData:any[];
  page:{
    pageIndex:number,
    pageSize:number,
    count:number
  };
  searchParams:{
    majorId?:number,
    gradeId?:number,
    courseName?:number,
  };
  majorOptions:any;
  gradeOptions:any;
  name:number;

  grades:Grade[];
  majors:Major[];

  modalAdd={
    visible:false,
    submitted:false,
    data:new Course(),
    majorOptions:[],
    gradeOptions:[]
  };
  modalEdit={
    visible:false,
    submitted:false,
    data:new Course(),
    majorOptions:[],
    gradeOptions:[]
  };
  constructor(private courseSvc:CourseService,private pop:PopService){
    this.resetParams();
    this.getGrades();
    this.getMajors();
    this.page={
      pageIndex:0,
      pageSize:10,
      count:0
    };
  }
  getGrades(){
    this.courseSvc.getGrades()
      .then((data)=>{
        if(data.length) {
          this.grades= data;
          let grade=new Grade();
          grade.gradeId=-1;
          grade.name='不限';
          this.gradeOptions=[grade].concat(this.grades);
        }
      })
  }
  getMajors(){
    this.courseSvc.getMajors()
      .then((data)=>{
        if(data.length) {
          this.majors = data;
          let major=new Major();
          major.majorId=-1;
          major.major='不限';
          this.majorOptions=[major].concat(this.majors);
        }
      })
  }
  search(){
    this.page.pageIndex=0;
    this.page.count=1;
    this.tableData=[];
    this.query();
  }
  query(){
    let params={
      majorId:this.searchParams.majorId,
      gradeId:this.searchParams.gradeId,
      courseName:this.searchParams.courseName,
      pageIndex:this.page.pageIndex,
      pageSize:this.page.pageSize
    };
    this.courseSvc.query(params)
      .then((res)=>{
        console.log(res);
        this.tableData=res.items;
        this.page.count=res.count;
      });
  }

  resetParams(){
    this.searchParams={
      majorId:-1,
      gradeId:-1
    }
  }


  testCourse(course:Course):boolean{
    if(!course.courseName){
      this.pop.error({
        text:'请输入课程名称！'
      });
      return false;
    }else if(course.gradeId==-1){
      this.pop.error({
        text:'请选择年级！'
      });
      return false;
    }else if(course.majorId==-1){
      this.pop.error({
        text:'请选择专业！'
      });
      return false;
    }
    return true;
  }
  showAddModal(){
    this.modalAdd.submitted=false;
    this.modalAdd.data=new Course();
    this.modalAdd.data.gradeId=-1;
    this.modalAdd.data.majorId=-1;
    let grade=new Grade();
    grade.gradeId=-1;
    grade.name='请选择';
    let major=new Major();
    major.majorId=-1;
    major.major='请选择';
    this.gradeOptions.unshift(grade);
    this.majorOptions.unshift(major);

    this.modalAdd.gradeOptions=[grade].concat(this.grades);
    this.modalAdd.majorOptions=[major].concat(this.majors);
    this.modalAdd.visible=true;
  }
  closeAddModal(){
    this.modalAdd.visible=false;
  }
  add(){
    if(this.testCourse(this.modalAdd.data)){
      this.modalAdd.submitted=true;
      this.courseSvc.add(this.modalAdd.data)
        .then((data)=>{
          this.modalAdd.submitted=false;
          this.modalAdd.visible=false;
          if(data.status){
            this.pop.info({
              text:'添加成功！'
            });
            this.query();
          }else{
            this.pop.info({
              title:'添加失败！',
              text:data.message
            });
          }
        });
    }
  }
  showEditModal(course:Course){
    this.modalEdit.submitted=false;
    this.modalEdit.data=new Course();
    let grade=new Grade();
    grade.gradeId=-1;
    grade.name='请选择';
    let major=new Major();
    major.majorId=-1;
    major.major='请选择';
    this.gradeOptions.unshift(grade);
    this.majorOptions.unshift(major);
    this.modalEdit.gradeOptions=[grade].concat(this.grades);
    this.modalEdit.majorOptions=[major].concat(this.majors);
    this.modalEdit.data.courseId=course.courseId;
    this.modalEdit.data.courseName=course.courseName;
    this.modalEdit.data.gradeId=course.gradeId;
    this.modalEdit.data.majorId=course.majorId;
    this.modalEdit.visible=true;
  }
  closeEditModal(){
    this.modalEdit.visible=false;
  }
  update(){
    if(this.testCourse(this.modalEdit.data)){
      this.modalEdit.submitted=true;
      this.courseSvc.update(this.modalEdit.data)
        .then((data)=>{
          this.modalEdit.submitted=false;
          this.modalEdit.visible=false;
          if(data.status){
            this.pop.info({
              text:'修改成功！'
            });
            this.query();
          }else{
            this.pop.info({
              title:'修改失败！',
              text:data.message
            });
          }
        });
    }
  }
  remove(id:number){
    this.pop.confirm({
      text:'确定要删除这个课程？',
      closeOnConfirm:false,
      showLoaderOnConfirm:true
    }).onConfirm(()=>{
      this.courseSvc.remove(id)
        .then((data)=>{
          if(data.status){
            this.pop.info({
              text:'删除成功！'
            });
            this.query();
          }else{
            this.pop.info({
              title:'删除失败！',
              text:data.message
            });
          }
        });
    })
  }














}
